package com.jkoss.wine.merchant.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.merchant.entity.MerchantProduct;
import com.jkoss.wine.merchant.entity.MerchantProductNorm;
import com.jkoss.wine.merchant.service.IMerchantProductService;
import com.jkoss.wine.merchant.service.IMerchantProductNormService;
import com.jkoss.wine.system.entity.MerchantEmployees;

/**
 * 前端控制器
 * 
 * @Author Ning
 * @Version 1.0, 2018-09-28
 * @See
 * @Since com.jkoss.mengal.merchant.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("merchant/merchantProductNorm")
public class MerchantProductNormController extends BaseController {

	@Autowired
	private IMerchantProductNormService iMerchantProductNormService;
	@Autowired
	private IMerchantProductService iMerchantProductService;

	/**
	 * 列表
	 * 
	 * @param dwzPageBean
	 * @param pdid
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/merchant/merchantProductNorm/list")
	public String list(DwzPageBean dwzPageBean, String pdid, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().ne("del", 1).eq("merchant_product_id", pdid);
		Page resultPage = iMerchantProductNormService.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		map.put("pdid", pdid);
		return "merchant/merchantProductNorm/list";
	}

	/**
	 * 去新增
	 * 
	 * @param request
	 * @param pdid
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/merchant/merchantProductNorm/toInsert")
	public String toInsert(HttpServletRequest request, String pdid, HttpServletResponse response, ModelMap map) {
		map.put("productid", pdid);
		return "merchant/merchantProductNorm/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/merchant/merchantProductNorm/toInsert")
	@ResponseBody
	public Object insert(@Valid MerchantProductNorm merchant_product_norm, String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 添加时间
		merchant_product_norm.setAddTime(CommonUtil.getDate());
		// 操作人
		MerchantEmployees employees = new MerchantEmployees();
		merchant_product_norm.setOperator(getUserId());
		// 是否删除 0-未删除，1-已删除
		merchant_product_norm.setDel(0);
		// 规格图片URL
		merchant_product_norm.setNormPicUrl("123");
		if (iMerchantProductNormService.insertMerchantProductNorm(merchant_product_norm)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/merchant/merchantProductNorm/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iMerchantProductNormService.selectById(id));
		return "merchant/merchantProductNorm/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/merchant/merchantProductNorm/toUpdate")
	@ResponseBody
	public Object update(@Valid MerchantProductNorm merchant_product_norm, HttpServletResponse response) throws Exception {

		// List<MerchantProductNorm> mNorms =
		// iMerchantProductNormService.selectList(null);
		// for (MerchantProductNorm merchantProductNorm : mNorms) {
		// if(merchant_product_norm.getNormName().equals(merchantProductNorm.getNormName())
		// ||
		// merchant_product_norm.getProductNum().equals(merchantProductNorm.getProductNum()))
		// {
		// throw new Exception("该商品已存在");
		// }
		// }
		if (iMerchantProductNormService.updateProductNorm(merchant_product_norm)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/merchant/merchantProductNorm/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (iMerchantProductNormService.deleteMerchantProductNorm(id)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 查询商品规格
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getMerchantProductNorm")
	@ResponseBody
	public Object getMerchantProductNorm(String pnid, HttpServletRequest request, HttpServletResponse response) throws Exception {
		MerchantProductNorm merchantProductNorm = iMerchantProductNormService.selectById(pnid);
		if (!CommonUtil.isBlank(merchantProductNorm)) {
			return ajaxSuccess(merchantProductNorm);
		} else {
			return ajaxError();
		}
	}
}
